//
//  JWTNavTitleView.m
//  JWT
//
//  Created by ndm on 17/2/6.
//  Copyright © 2017年 com.jwtsoft. All rights reserved.
//

#import "JWTNavTitleView.h"
#import "JWTStore.h"

static NSString *storeCell_identifier = @"storeCell_identifier";

@interface JWTNavTitleView ()<UITableViewDelegate,UITableViewDataSource,UIGestureRecognizerDelegate>

@end

@implementation JWTNavTitleView
SHARED_INSTANCE_IMPLEMENTATION(JWTNavTitleView);
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame] ;
    if (self) {
        _storeArray = [[NSArray alloc] init];
        
        _navTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, UISCREEN_WIDTH*0.5, 30)];
        _navTitleLabel.textColor = RGB_WhiteBackGround;
        _navTitleLabel.textAlignment = NSTextAlignmentCenter;
        _navTitleLabel.font = [UIFont boldSystemFontOfSize:18.0f];
        [self addSubview:_navTitleLabel];
        
        self.frame = CGRectMake(self.left, self.top, _navTitleLabel.width, 30);
        
//        _navImageView = [[ UIImageView alloc] initWithFrame:CGRectMake(_navTitleLabel.right+2, _navImageView.top, 20, 20)];
//        _navImageView.contentMode = UIViewContentModeScaleAspectFit;
//        [_navImageView setImage:[UIImage imageNamed:@"nav_address"]];
//        [self addSubview:_navImageView];
//        _navImageView.center = CGPointMake(_navImageView.center.x, _navTitleLabel.center.y);
//        
//        self.frame = CGRectMake(self.left, self.top, _navTitleLabel.width+_navImageView.width, 30);
        
//        _navBtn = [[UIButton alloc] initWithFrame:self.frame];
//        _navBtn.selected = NO;
//        [self addSubview:_navBtn];
//        
//        WS(weakSelf);
//        
//        [_navBtn addButtonBlockControlEvent:UIControlEventTouchUpInside withBlock:^(id sender) {
//            UIButton *btn = (UIButton *)sender;
//            if (btn.selected == NO) {
//                btn.selected = YES;
//                [weakSelf initWithShowView];
//            }else{
//                btn.selected = NO;
//                [weakSelf removeSubView];
//            }
//            
//        }];
        
//        [self getJWTStore];
    }
    return self;
}

-(void) tapBackgroundView:(UITapGestureRecognizer *)rec{
    [self removeSubView];
    _navBtn.selected = NO;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    
    if ([touch.view isKindOfClass:[UIButton class]]){
        return NO;
    }
    if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {//判断如果点击的是tableView的cell，就把手势给关闭了
        return NO;//关闭手势
    }
    return YES;
}

/**
 初始化视图
 */
-(void) initWithShowView{
    _backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, UISCREEN_WIDTH, UISCREEN_HEIGHT-64)];
    _backgroundView.backgroundColor = RGBA(0, 0, 0, 0.4);
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [window addSubview:_backgroundView];
    //添加单击手势
    UITapGestureRecognizer *tapBackgroundView = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBackgroundView:)];
    tapBackgroundView.delegate = self;
    [_backgroundView addGestureRecognizer:tapBackgroundView];
    
    [self initWithTableView];
}

/**
 移除视图
 */
-(void) removeSubView{
    [_tableView removeFromSuperview ];
    _tableView = nil;
    [_backgroundView removeFromSuperview];
    _backgroundView = nil;
}

-(void) initWithTableView{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(self.left, 0, self.width, 5*44)];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [_backgroundView addSubview:_tableView];
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:storeCell_identifier];
}

#pragma mark - 表格视图数据源代理方法

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _storeArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:storeCell_identifier forIndexPath:indexPath];
    JWTStore *store = [JWTStore mj_objectWithKeyValues:_storeArray[indexPath.row]];
    cell.textLabel.text = store.nickname;
    
    return  cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self removeSubView];
    _navBtn.selected = NO;
//    JWTStore *store = [JWTStore mj_objectWithKeyValues:_storeArray[indexPath.row]];
//    _navTitleLabel.text = store.text;
//    APPDELEGATE.storeID = store.value;
//    if (self.getAreaInfo) {
//        self.getAreaInfo(APPDELEGATE.storeID);
//    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//    static UITableViewCell *cell = nil;
//    static dispatch_once_t onceToken;
//    dispatch_once(&onceToken, ^{
//        cell = [tableView dequeueReusableCellWithIdentifier:storeCell_identifier];
//    });
//    return cell.height;
    return 44;
}

/**
 获取门店信息
 */
-(void) getJWTStore{
    NSString *urlString = [[JWTAPPHttpsUrl sharedInstance] getJWTStoreComboBox];
    [HUD showUIBlockingIndicator];
    [AFNHttpsRequest requestWithUrl:urlString WithParam:nil WithGetDataDidFinishBlock:^(id model, NSURLSessionDataTask *task, NSError *error) {
        [HUD hideUIBlockingIndicator];
        if (model) {
            if ([[model objectForKey:@"code"]integerValue] == 200) {
                NSLog(@"%@",model);
                _storeArray = [model objectForKey:@"data"];
                if (_storeArray.count > 0) {
                    JWTStore *store = [JWTStore mj_objectWithKeyValues:_storeArray[0]];
                    _navTitleLabel.text = store.nickname;
                    APPDELEGATE.storeID = store.storeid;
                    
//                    if (self.getAreaInfo) {
//                        self.getAreaInfo(APPDELEGATE.storeID);
//                    }
                }else{
                    _navTitleLabel.text = @"";
                }
                _tableView.frame = CGRectMake(self.left, 0, self.width, _storeArray.count*44);
                [_tableView reloadData];
            }else if ([[model objectForKey:@"200"]integerValue] == 500){
                _tableView.frame = CGRectMake(self.left, 0, self.width, _storeArray.count*44);
                [HUD showToast:@"获取门店信息失败"];
            }
        }
    }];
}

@end
